package com.jzo2o.customer.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.customer.constants.CommonConstants;
import com.jzo2o.customer.mapper.AgencyCertificationAuditMapper;
import com.jzo2o.customer.model.domain.AgencyCertification;
import com.jzo2o.customer.model.domain.AgencyCertificationAudit;
import com.jzo2o.customer.model.dto.request.AgencyCertificationAuditPageQueryReqDTO;
import com.jzo2o.customer.model.dto.request.CertificationAuditReqDTO;
import com.jzo2o.customer.model.dto.response.AgencyCertificationAuditResDTO;
import com.jzo2o.customer.service.IAgencyCertificationAuditService;
import com.jzo2o.customer.service.IAgencyCertificationService;
import com.jzo2o.mvc.utils.UserContext;
import com.jzo2o.mysql.utils.PageUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Objects;

/**
 * \* User: chy
 * \* Date: 2025/9/26  18:56
 * \* Description:
 * \
 */
@Service
public class AgencyCertificationAuditServiceImpl extends ServiceImpl<AgencyCertificationAuditMapper, AgencyCertificationAudit> implements IAgencyCertificationAuditService {

    @Resource
    private IAgencyCertificationService agencyCertificationService;
    /**
     * 运营端认证机构信息分页查询
     *
     * @param queryReqDTO
     * @return
     */
    @Override
    public PageResult<AgencyCertificationAuditResDTO> queryAgencyCertificationAuditPage(AgencyCertificationAuditPageQueryReqDTO queryReqDTO) {
        // 分页查询
        Page<AgencyCertificationAudit> page = new Page<>(queryReqDTO.getPageNo(), queryReqDTO.getPageSize());
        // 查询条件
        LambdaUpdateWrapper<AgencyCertificationAudit> wrapper = new LambdaUpdateWrapper<AgencyCertificationAudit>()
                .eq(StringUtils.hasText(queryReqDTO.getName()), AgencyCertificationAudit::getName, queryReqDTO.getName())
                .eq(StringUtils.hasText(queryReqDTO.getLegalPersonName()), AgencyCertificationAudit::getLegalPersonName, queryReqDTO.getLegalPersonName())
                .eq(queryReqDTO.getAuditStatus() != null, AgencyCertificationAudit::getAuditStatus, queryReqDTO.getAuditStatus())
                .eq(queryReqDTO.getCertificationStatus() != null, AgencyCertificationAudit::getCertificationStatus, queryReqDTO.getCertificationStatus());
        Page<AgencyCertificationAudit> pageResult = this.page(page, wrapper);
        return PageUtils.toPage(pageResult, AgencyCertificationAuditResDTO.class);

    }
    /**
     * 审核机构认证信息
     *
     * @param id
     */
    @Override
    @Transactional
    public void audit(Long id, CertificationAuditReqDTO auditReqDTO) {
        // 1.构建审核信息
        AgencyCertificationAudit audit = new AgencyCertificationAudit();
        audit.setId(id);
        audit.setAuditStatus(CommonConstants.AUDIT_STATUS_PASS);
        //如果拒绝，则设置拒绝原因和为未审核审核状态
        if(Objects.equals(auditReqDTO.getCertificationStatus(), CommonConstants.CERTIFICATION_STATUS_FAIL)){
            audit.setRejectReason(auditReqDTO.getRejectReason());
        }
        audit.setCertificationStatus(auditReqDTO.getCertificationStatus());//认证状态，1：认证中，2：认证成功，3认证失败
        audit.setAuditorId(UserContext.currentUserId());//审核人id
        audit.setAuditorName(UserContext.currentUser().getName());//审核人姓名
        audit.setAuditTime(LocalDateTime.now());
        this.updateById(audit);
        audit  =  this.getById( id);

        //2.构建机构信息
        AgencyCertification agencyCertification = new AgencyCertification();
        agencyCertification.setId(audit.getServeProviderId());//机构id
        agencyCertification.setCertificationStatus(audit.getCertificationStatus());//认证状态，1：认证中，2：认证成功，3认证失败
        agencyCertification.setCertificationTime(audit.getAuditTime());//认证时间
        agencyCertificationService.updateById(agencyCertification);

    }
}
